package com.ebb.activities;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ebb.R;
import com.ebb.classes.EBBActivity;
import com.ebb.classes.EBBAlert;
import com.ebb.common.EBBCommonUtil;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;

public class EBBSignUp extends EBBActivity implements OnClickListener {
    private EditText etUsername;
    private EditText etNickName;
    private EditText etPassword;
    private EditText etPasswordConfirm;
    private Button btnSignUp;
    private static final String TAG = EBBSignUp.class.getCanonicalName();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        this.etUsername = (EditText) findViewById(R.id.username_value);
        this.etNickName = (EditText) findViewById(R.id.nickname_value);
        this.etPassword = (EditText) findViewById(R.id.password_value);
        this.etPasswordConfirm = (EditText) findViewById(R.id.password_confirm_value);

        this.btnSignUp = (Button) findViewById(R.id.signup_button);
        this.btnSignUp.setId(0x2001);
        this.btnSignUp.setOnClickListener(this);
        
        etUsername.requestFocus();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case 0x2001:
			Log.i(TAG, "[success] do sign up");
            doSignUp();
            break;
        }
    }

    private void doSignUp() {
        String title = "Sign Up";
        if(EBBCommonUtil.isEmptyEditText(etUsername)) {
            EBBAlert.message(0, title, "Please input your username. ");
            etUsername.requestFocus();
            return;
        } else if(EBBCommonUtil.isEmptyEditText(etNickName)){
            EBBAlert.message(0, title, "Please input your nickname. ");
            etNickName.requestFocus();
            return;            
        } else if(EBBCommonUtil.isEmptyEditText(etPassword)){
            EBBAlert.message(0, title, "Please input your password. ");
            etPassword.requestFocus();
            return;            
        } else if(EBBCommonUtil.isEmptyEditText(etPasswordConfirm)){
            EBBAlert.message(0, title, "Please input your password confirm. ");
            etPasswordConfirm.requestFocus();
            return;            
        } else if (!EBBCommonUtil.isEqualBothEditText(etPassword, etPasswordConfirm)) {
            EBBAlert.message(0, title, "Your confirmed password did not match your password field.");
            return;
        }
        
        ParseUser user = new ParseUser();
        user.setUsername(etUsername.getText().toString().trim());
        user.setPassword(etPassword.getText().toString().trim());
        user.put("nickname", etNickName.getText().toString().trim());
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(ParseException e) {
                if(e != null) {
                    EBBAlert.message(0, "Sign Up Error", e.getMessage());
                    return;
                }
                setIntent("com.ebb.EBBLogin");
            }
        }); 
    }
}
